﻿using System.Windows.Media;
using System.Xml.Linq;
using FirstFloor.Documents.Annotations.Storage;

namespace FirstFloor.Documents.Presentation.Annotations
{
    /// <summary>
    /// Reads and writes underline annotations.
    /// </summary>
    /// <remarks>
    /// The underline factory provides persistence to an XML based store.
    /// </remarks>
    public class UnderlineFactory
        : TextAnnotationFactory<Underline>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="UnderlineFactory"/> class.
        /// </summary>
        public UnderlineFactory()
            : base(Underline.TypeUnderline)
        {
        }

        /// <summary>
        /// Creates the annotation.
        /// </summary>
        /// <returns>The annotation.</returns>
        public override Underline CreateAnnotation()
        {
            return new Underline();
        }

        /// <summary>
        /// Reads the annotation properties.
        /// </summary>
        /// <param name="annotation">The annotation.</param>
        /// <param name="source">The source.</param>
        protected override void ReadAnnotationProperties(Underline annotation, XElement source)
        {
            Color c;
            if (ParserHelper.TryParseColor((string)source.Attribute("Color"), out c)) {
                annotation.Color = c;
            }
        }

        /// <summary>
        /// Writes the annotation properties.
        /// </summary>
        /// <param name="annotation">The annotation.</param>
        /// <param name="target">The target.</param>
        protected override void WriteAnnotationProperties(Underline annotation, XElement target)
        {
            target.Add(new XAttribute("Color", annotation.Color.ToString()));
        }
    }
}
